﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Input;
using System.Runtime.InteropServices;

namespace WinPad
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        GamePadState gamePad;

        bool leftClickDown = false;
        bool rightClickDown = false;
        bool startClickDown = false;

        bool speedUpClickDown = false;
        bool speedDownClickDown = false;

        int mouseSpeed = 20;

        public Form1()
        {
            InitializeComponent();
            gamePad = GamePad.GetState(0);
            this.Cursor = new Cursor(Cursors.Arrow.Handle);
        }

        private void Update_Tick(object sender, EventArgs e)
        {
            gamePad = GamePad.GetState(GamePadIndex.GetIntNumber() - 1);

            Point mousePos = System.Windows.Forms.Control.MousePosition;

            #region Pos
            float x = gamePad.ThumbSticks.Left.X * mouseSpeed;
            float y = gamePad.ThumbSticks.Left.Y * mouseSpeed;

            Cursor.Position = new Point(mousePos.X + (int)x, mousePos.Y + (int)y);
            mousePos = System.Windows.Forms.Control.MousePosition;
            #endregion

            #region Click
            //Left
            if (leftClickDown)
            {
                if (gamePad.Buttons.Y == OpenTK.Input.ButtonState.Released)
                {
                    leftClickDown = false;
                    mouse_event(MOUSEEVENTF_LEFTUP, (uint)mousePos.X, (uint)mousePos.Y, 0, 0);
                }
            }
            else
                if (gamePad.Buttons.Y == OpenTK.Input.ButtonState.Pressed)
                {
                    leftClickDown = true;
                    mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)mousePos.X, (uint)mousePos.Y, 0, 0);
                }

            //Right
            if (rightClickDown)
            {
                if (gamePad.Buttons.B == OpenTK.Input.ButtonState.Released)
                {
                    rightClickDown = false;
                    mouse_event(MOUSEEVENTF_RIGHTUP, (uint)mousePos.X, (uint)mousePos.Y, 0, 0);
                }
            }
            else
                if (gamePad.Buttons.B == OpenTK.Input.ButtonState.Pressed)
                {
                    rightClickDown = true;
                    mouse_event(MOUSEEVENTF_RIGHTDOWN, (uint)mousePos.X, (uint)mousePos.Y, 0, 0);
                }
            #endregion

            //Start
            if (startClickDown)
            {
                if (gamePad.Buttons.A == OpenTK.Input.ButtonState.Released)
                {
                    startClickDown = false;
                    this.Visible = !this.Visible;
                }
            }
            else
                if (gamePad.Buttons.A == OpenTK.Input.ButtonState.Pressed)
                    startClickDown = true;

            //Mouse Speed
            #region Change Mouse Speed
            if (speedUpClickDown)
            {
                if (gamePad.Buttons.RightShoulder == OpenTK.Input.ButtonState.Released)
                    speedUpClickDown = false;
            }
            else
                if (gamePad.Buttons.RightShoulder == OpenTK.Input.ButtonState.Pressed)
                {
                    speedUpClickDown = true;
                    mouseSpeed += 5;
                }

            if (speedDownClickDown)
            {
                if (gamePad.Buttons.LeftShoulder == OpenTK.Input.ButtonState.Released)
                    speedDownClickDown = false;
            }
            else
                if (gamePad.Buttons.LeftShoulder == OpenTK.Input.ButtonState.Pressed)
                {
                    speedDownClickDown = true;
                    mouseSpeed -= 5;
                }
            #endregion
        }

        private void Hide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
    }
}
